package com.alan.netty.nio.channels;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 文件通道，用于文件的读和写
 *
 * @author weiBin
 * @date 2018/11/7
 */
public class FileChannelAccess {
    public static void main(String[] args) throws IOException {
        String infile = "D:\\alan\\netty-test\\src\\main\\resources\\news.txt";
        String outfile = "D:\\alan\\netty-test\\src\\main\\resources\\news.copy.txt";
        FileInputStream fin = new FileInputStream(infile);
        FileChannel fcin = fin.getChannel();
        FileOutputStream fout = new FileOutputStream(outfile);
        FileChannel fcout = fout.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true) {
            buffer.clear();
            int i = fcin.read(buffer);
            if (i == -1) {
                break;
            }
            buffer.flip();
            fcout.write(buffer);
        }
        fcin.close();
        fin.close();
        fcout.close();
        fout.close();
    }
}
